import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'common_avatar.dart';

/// 通用列表项组件
/// 提供统一的列表项样式，支持主题切换
class CommonListItem extends StatelessWidget {
  /// 标题
  final String title;

  /// 副标题
  final String? subtitle;

  /// 描述信息
  final String? description;

  /// 左侧图标
  final Widget? leading;

  /// 右侧组件
  final Widget? trailing;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示分割线
  final bool showDivider;

  /// 内边距
  final EdgeInsetsGeometry? padding;

  /// 背景颜色
  final Color? backgroundColor;

  const CommonListItem({
    super.key,
    required this.title,
    this.subtitle,
    this.description,
    this.leading,
    this.trailing,
    this.onTap,
    this.showDivider = false,
    this.padding,
    this.backgroundColor,
  });

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Material(
          color: backgroundColor ?? Colors.transparent,
          child: InkWell(
            onTap: onTap,
            child: Padding(
              padding: padding ??
                  EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
              child: Row(
                children: [
                  if (leading != null) ...[
                    leading!,
                    SizedBox(width: 12.w),
                  ],
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          title,
                          style: TextStyle(
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w500,
                            color: context.theme.textTheme.titleMedium?.color,
                          ),
                        ),
                        if (subtitle != null) ...[
                          SizedBox(height: 4.h),
                          Text(
                            subtitle!,
                            style: TextStyle(
                              fontSize: 14.sp,
                              color: context.theme.textTheme.bodyMedium?.color,
                            ),
                          ),
                        ],
                        if (description != null) ...[
                          SizedBox(height: 4.h),
                          Text(
                            description!,
                            style: TextStyle(
                              fontSize: 12.sp,
                              color: context.theme.textTheme.bodySmall?.color,
                            ),
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                          ),
                        ],
                      ],
                    ),
                  ),
                  if (trailing != null) ...[
                    SizedBox(width: 12.w),
                    trailing!,
                  ],
                ],
              ),
            ),
          ),
        ),
        if (showDivider)
          Divider(
            height: 1,
            color: context.theme.dividerColor,
            indent: leading != null ? 60.w : 16.w,
            endIndent: 16.w,
          ),
      ],
    );
  }
}

/// 患者列表项组件
class PatientListItem extends StatelessWidget {
  /// 患者姓名
  final String name;

  /// 患者年龄
  final int age;

  /// 患者性别
  final String gender;

  /// 患者电话
  final String? phone;

  /// 最后就诊时间
  final String? lastVisit;

  /// 头像URL
  final String? avatarUrl;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示分割线
  final bool showDivider;

  const PatientListItem({
    super.key,
    required this.name,
    required this.age,
    required this.gender,
    this.phone,
    this.lastVisit,
    this.avatarUrl,
    this.onTap,
    this.showDivider = true,
  });

  @override
  Widget build(BuildContext context) {
    return CommonListItem(
      title: name,
      subtitle: '$age岁 · $gender',
      description: lastVisit != null ? '最后就诊：$lastVisit' : null,
      leading: CommonAvatar(
        name: name,
        imageUrl: avatarUrl,
        size: 40.w,
      ),
      trailing: phone != null
          ? Column(
              crossAxisAlignment: CrossAxisAlignment.end,
              children: [
                Icon(
                  Icons.phone,
                  size: 16.w,
                  color: context.theme.primaryColor,
                ),
                SizedBox(height: 4.h),
                Text(
                  phone!,
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: context.theme.textTheme.bodySmall?.color,
                  ),
                ),
              ],
            )
          : Icon(
              Icons.arrow_forward_ios,
              size: 16.w,
              color: context.theme.textTheme.bodySmall?.color,
            ),
      onTap: onTap,
      showDivider: showDivider,
    );
  }
}

/// 商品列表项组件
class ProductListItem extends StatelessWidget {
  /// 商品名称
  final String name;

  /// 商品价格
  final String price;

  /// 商品规格
  final String? specification;

  /// 供应商
  final String? supplier;

  /// 商品图片
  final String? imageUrl;

  /// 库存数量
  final int? stock;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示分割线
  final bool showDivider;

  const ProductListItem({
    super.key,
    required this.name,
    required this.price,
    this.specification,
    this.supplier,
    this.imageUrl,
    this.stock,
    this.onTap,
    this.showDivider = true,
  });

  @override
  Widget build(BuildContext context) {
    return CommonListItem(
      title: name,
      subtitle: specification,
      description: supplier != null ? '供应商：$supplier' : null,
      leading: Container(
        width: 50.w,
        height: 50.w,
        decoration: BoxDecoration(
          color: context.theme.cardColor,
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color: context.theme.dividerColor,
            width: 1,
          ),
        ),
        child: imageUrl != null
            ? ClipRRect(
                borderRadius: BorderRadius.circular(8.r),
                child: Image.network(
                  imageUrl!,
                  fit: BoxFit.cover,
                  errorBuilder: (context, error, stackTrace) => Icon(
                    Icons.medical_services,
                    color: context.theme.primaryColor,
                    size: 24.w,
                  ),
                ),
              )
            : Icon(
                Icons.medical_services,
                color: context.theme.primaryColor,
                size: 24.w,
              ),
      ),
      trailing: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Text(
            price,
            style: TextStyle(
              fontSize: 16.sp,
              fontWeight: FontWeight.bold,
              color: context.theme.primaryColor,
            ),
          ),
          if (stock != null) ...[
            SizedBox(height: 4.h),
            Text(
              '库存：$stock',
              style: TextStyle(
                fontSize: 12.sp,
                color: stock! > 0
                    ? context.theme.textTheme.bodySmall?.color
                    : Colors.red,
              ),
            ),
          ],
        ],
      ),
      onTap: onTap,
      showDivider: showDivider,
    );
  }
}

/// 课程列表项组件
class CourseListItem extends StatelessWidget {
  /// 课程标题
  final String title;

  /// 课程描述
  final String? description;

  /// 讲师
  final String? instructor;

  /// 课程时长
  final String? duration;

  /// 课程封面
  final String? coverUrl;

  /// 是否已完成
  final bool isCompleted;

  /// 进度百分比
  final double? progress;

  /// 点击事件
  final VoidCallback? onTap;

  /// 是否显示分割线
  final bool showDivider;

  const CourseListItem({
    super.key,
    required this.title,
    this.description,
    this.instructor,
    this.duration,
    this.coverUrl,
    this.isCompleted = false,
    this.progress,
    this.onTap,
    this.showDivider = true,
  });

  @override
  Widget build(BuildContext context) {
    return CommonListItem(
      title: title,
      subtitle: instructor != null ? '讲师：$instructor' : null,
      description: description,
      leading: Container(
        width: 60.w,
        height: 45.w,
        decoration: BoxDecoration(
          color: context.theme.cardColor,
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color: context.theme.dividerColor,
            width: 1,
          ),
        ),
        child: coverUrl != null
            ? ClipRRect(
                borderRadius: BorderRadius.circular(8.r),
                child: Image.network(
                  coverUrl!,
                  fit: BoxFit.cover,
                  errorBuilder: (context, error, stackTrace) => Icon(
                    Icons.play_circle_outline,
                    color: context.theme.primaryColor,
                    size: 24.w,
                  ),
                ),
              )
            : Icon(
                Icons.play_circle_outline,
                color: context.theme.primaryColor,
                size: 24.w,
              ),
      ),
      trailing: Column(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          if (isCompleted)
            Container(
              padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 2.h),
              decoration: BoxDecoration(
                color: Colors.green.withOpacity(0.1),
                borderRadius: BorderRadius.circular(12.r),
              ),
              child: Text(
                '已完成',
                style: TextStyle(
                  fontSize: 10.sp,
                  color: Colors.green,
                  fontWeight: FontWeight.w500,
                ),
              ),
            )
          else if (progress != null)
            Column(
              children: [
                Text(
                  '${(progress! * 100).toInt()}%',
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: context.theme.textTheme.bodySmall?.color,
                  ),
                ),
                SizedBox(height: 4.h),
                Container(
                  width: 40.w,
                  height: 4.h,
                  decoration: BoxDecoration(
                    color: context.theme.dividerColor,
                    borderRadius: BorderRadius.circular(2.r),
                  ),
                  child: FractionallySizedBox(
                    alignment: Alignment.centerLeft,
                    widthFactor: progress,
                    child: Container(
                      decoration: BoxDecoration(
                        color: context.theme.primaryColor,
                        borderRadius: BorderRadius.circular(2.r),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          if (duration != null) ...[
            SizedBox(height: 4.h),
            Text(
              duration!,
              style: TextStyle(
                fontSize: 12.sp,
                color: context.theme.textTheme.bodySmall?.color,
              ),
            ),
          ],
        ],
      ),
      onTap: onTap,
      showDivider: showDivider,
    );
  }
}
